package com.zztl.infiniteBand.ui.activity

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.adapter.CountryCodeAdapter
import com.zztl.infiniteBand.base.BaseActivity
import com.zztl.infiniteBand.model.CountryCodeBean
import com.zztl.infiniteBand.net.converter.DataConverter
import com.zztl.infiniteBand.storage.BillPreference
import com.zztl.infiniteBand.storage.Constans
import com.zztl.infiniteBand.storage.PreferenceKeys
import com.zztl.infiniteBand.ui.view.SideBar
import com.zztl.infiniteBand.ui.view.recycleview.SimpleItemDecoration
import com.zztl.infiniteBand.util.AlphaComparator
import kotlinx.android.synthetic.main.activity_country_code.*
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder
import java.util.*

class CountryCodeActivity : BaseActivity() {
    lateinit var codeBean: CountryCodeBean
    lateinit var codeAdapter: CountryCodeAdapter
    var list = arrayListOf<CountryCodeBean.DataBean>()
    override fun setLayoutId(): Int = R.layout.activity_country_code

    override fun initBase() {
//        setTitle(getString(R.string.ѡ����Һ͵���))
        initRecycleView()
        initData()
        sideBar.setOnTouchingLetterChangedListener(object : SideBar.OnTouchingLetterChangedListener {
            override fun onTouchingLetterChanged(s: String) {
                for (i in 0 until codeAdapter.data.size) {
                    if (codeAdapter.getItem(i)?.firstLetter.equals(s, true)) {
                        recycleView.scrollToPosition(i)
                        (recycleView.getLayoutManager() as LinearLayoutManager).apply {
                            scrollToPositionWithOffset(i, 0)
                        }


                        break
                    }
                }
            }
        })
    }

    private fun initRecycleView() {

        recycleView.layoutManager = LinearLayoutManager(this).apply { orientation = LinearLayoutManager.VERTICAL }
        recycleView.addItemDecoration(SimpleItemDecoration(this@CountryCodeActivity))
        codeAdapter = CountryCodeAdapter(R.layout.item_country_code, list).apply {
            recycleView.adapter = this
        }
        codeAdapter.setOnItemClickListener { adapter, view, position ->
            Intent().apply {
                putExtra(Constans.COUNTRY_CODE_STR, codeAdapter.data.get(position).countryCode)
                setResult(Activity.RESULT_OK, this)
                BillPreference.putApp(PreferenceKeys.COUNTYRCODE.name, codeAdapter.data.get(position).countryCode!!)
            }
            finish()
        }

    }

    private fun initData() {
        var line: String? = null
        var sb = StringBuilder()
        resources.assets.open("country.json").let {
            BufferedReader(InputStreamReader(it, "GBK")).use { read ->
                while (read.readLine().also { line = it } != null) {
                    sb.append(line)
                }
            }
        }
        val data = sb.toString()
        codeBean = DataConverter.instance.getSingleBean(data, CountryCodeBean::class.java)
        val comparator = AlphaComparator(this)
        Collections.sort(codeBean.data, comparator)
        codeBean.data?.apply {
            add(0, CountryCodeBean.DataBean("852", 2, "�й����"))
            add(0, CountryCodeBean.DataBean("81", 6, "�ձ�"))
            add(0, CountryCodeBean.DataBean("65", 5, "�¼���"))
            add(0, CountryCodeBean.DataBean("86", 1, "�й���½"))
            add(0, CountryCodeBean.DataBean("1", 15, "����"))
            add(0, CountryCodeBean.DataBean("853", 3, "�й�����"))
        }
        codeAdapter.setNewData(codeBean.data!!)
    }


}
